package com.cz.modules.jxc.controller;

import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cz.common.validator.ValidatorUtils;
import com.cz.modules.sys.entity.SysDeptEntity;
import com.cz.modules.sys.service.SysDeptService;
import com.cz.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cz.modules.jxc.entity.JxcShopEntity;
import com.cz.modules.jxc.service.JxcShopService;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.R;



/**
 * 品牌管理
 *
 * @author Cz
 * @email 
 * @date 2020-04-16 13:42:40
 */
@RestController
@RequestMapping("jxc/jxcshop")
public class JxcShopController {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private JxcShopService jxcShopService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jxc:jxcshop:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxcShopService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */  
    @RequestMapping("/info/{id}")
    @RequiresPermissions("jxc:jxcshop:info")
    public R info(@PathVariable("id") String id){
        JxcShopEntity jxcShop = jxcShopService.getById(id);
        List<SysDeptEntity> deptlist=sysDeptService.list();
        for (SysDeptEntity dept:deptlist) {
            if(jxcShop.getDeptId().equals(dept.getDeptId().toString())){
                jxcShop.setDeptName(dept.getName());
            }
        }
        return R.ok().put("jxcShop", jxcShop);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoByCode")
    public R infoByCode(@RequestParam Map<String, Object> params){
        String shopCode= (String) params.get("shopCode");
        String sellMasterDate= (String) params.get("sellMasterDate");
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(DateUtil.parse(sellMasterDate,"yyyy-MM-dd HH:mm:ss"));
        calendar.add(Calendar.SECOND,-10);
        String startTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
        calendar.add(Calendar.SECOND,20);
        String endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
        String st[]=startTime.split(" ");
        String stdate[]=st[0].split("-");
        String sttime[]=st[1].split(":");
        String et[]=endTime.split(" ");
        String etdate[]=et[0].split("-");
        String ettime[]=et[1].split(":");
        startTime=stdate[0]+stdate[1]+stdate[2]+"t"+sttime[0]+sttime[1]+sttime[2]+"z";
        endTime=etdate[0]+etdate[1]+etdate[2]+"t"+ettime[0]+ettime[1]+ettime[2]+"z";

        JxcShopEntity jxcShop = jxcShopService.getOne(new QueryWrapper<JxcShopEntity>().eq("shop_code",shopCode));
        return R.ok().put("jxcShop", jxcShop).put("startTime",startTime).put("endTime",endTime);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jxc:jxcshop:save")
    public R save(@RequestBody JxcShopEntity jxcShop){
        Date time=new Date();
        String userId=ShiroUtils.getUserId().toString();
        jxcShop.setCreator(userId);
        jxcShop.setCreateDate(time);
        jxcShop.setStatus("0");
        jxcShopService.save(jxcShop);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jxc:jxcshop:update")
    public R update(@RequestBody JxcShopEntity jxcShop){
        Date time=new Date();
        String userId=ShiroUtils.getUserId().toString();
        jxcShop.setUpdateDate(time);
        jxcShop.setUpdater(userId);
        ValidatorUtils.validateEntity(jxcShop);
        jxcShopService.updateById(jxcShop);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jxc:jxcshop:delete")
    public R delete(@RequestBody String[] ids){
        /*Date time=new Date();
        String userId=ShiroUtils.getUserId().toString();
        List<JxcShopEntity> shoplist=jxcShopService.list(
                new QueryWrapper<JxcShopEntity>()
                .in("id",ids)
        );
        for (JxcShopEntity shop:shoplist) {
            shop.setStatus("1");
            shop.setUpdateDate(time);
            shop.setUpdater(userId);
        }
        jxcShopService.updateBatchById(shoplist);*/
        jxcShopService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
    /**
     * 部门树
     */
    @RequestMapping("/deptTree")
    public R deptTree(){
        return R.ok().put("deptTree",jxcShopService.deptTree());
    }

}
